package online.shenme.day15.tcp;

import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class FTP_DAO {

    public void update(Socket accept) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        BufferedWriter bw = null;
        try {
            DatagramSocket service = new DatagramSocket(8868);
            byte[] dates = new byte[1024];
            DatagramPacket packet = new DatagramPacket(dates, dates.length);
            service.receive(packet);

            bis = new BufferedInputStream(accept.getInputStream());
            bos = new BufferedOutputStream(new FileOutputStream("javaadvance/src/online/shenme/day15/update/test" + new Date().getTime() + "." + new String(dates, 0, packet.getLength())));
            int b;
            while ((b = bis.read()) != -1) {
                bos.write(b);
                bos.flush();
            }

            bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bw.write("成功");
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}